package org.madgecko.basic.service.impl;

import org.madgecko.basic.dao.iface.ICommandDao;
import org.madgecko.basic.dao.iface.IModuleDao;
import org.madgecko.basic.model.Command;
import org.madgecko.basic.model.Module;
import org.madgecko.basic.model.Role;
import org.madgecko.basic.service.iface.ICommandService;
import org.madgecko.basic.service.iface.IModuleService;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class ModuleService implements IModuleService {

    @Inject
    private IModuleDao moduleDao;
	@Inject
	private ICommandDao commandDao;
	@Inject
	private ICommandService commandService;

	@Override
	public void add(Module module) {
		moduleDao.add(module);
	}

	@Override
	public void delete(int id) {
	    List<Integer> ids = this.listIdsByParent(id);
	    ids.add(id);
	    for(Integer i : ids){
            // 删除相应的command
            commandDao.deleteByModule(i);
            // 删除本身
            moduleDao.delete(i);
        }
	}

	@Override
	public Module load(int id) {
		return moduleDao.load(id);
	}

	@Override
	public void update(Module module) {
		moduleDao.update(module);
	}

	@Override
    public List<Module> listAllModule() {
        return moduleDao.listAllModule();
    }

	@Override
	public List<Module> listModuleByRoles(List<Integer> roleIds) {
		return moduleDao.listModuleByRoles(roleIds);
	}

	@Override
	public List<Module> listModuleByParent(int parentId) {
		return moduleDao.listModuleByParent(parentId);
	}

	public List<Integer> listIdsByParent(Integer parentId){
        return moduleDao.listIdsByParent(parentId);
    }
}
